package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2024-01-20
 * Time:18:48
 */
public class AddOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        int size = bookList.getUsedSize();
        //如果书架满了，那就不能添加
        if(size == bookList.Capacity) {
            System.out.println("书架已满，无法增加图书");
            return;
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = scanner.nextLine();
        System.out.println("请输入作者名字");
        String author = scanner.nextLine();
        System.out.println("请输入图书定价");
        int price = scanner.nextInt();
        System.out.println("请输入图书类型");
        String type = scanner.nextLine();
        Book book = new Book(name,author,price,type,false);

        //看书架是否已经有这本书了
        for(int i = 0;i < size;i++) {
            if(bookList.getBooks(i).equals(book)) {
                System.out.println("已经有这本书了");
                return;
            }
        }

        bookList.setBooks(book,size);  //把这本书放进下标为size的位置
        bookList.setUsedSize(size+1);
    }
}
